﻿//
// <author>David Nohejl</author>
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows;
using WPFTrees.Parser;

namespace WPFTrees.Layouts
{
    /// <summary>
    /// Nakresleni stromu ve kterem jsou hrany rovne cary.
    /// </summary>
    public class StraightLineBranchDrawer:IBranchDrawer
    {
        Brush branchBrush;        

        /// <summary>
        /// Vytvori novou instanci StraightLineBranchDrawer.
        /// </summary>
        /// <param name="branchBrush">Brush kterym se hrana nakresli.</param>
        public StraightLineBranchDrawer(Brush branchBrush)
        {
            this.branchBrush = branchBrush;            
        }

        /// <inheritdoc />        
        public System.Windows.FrameworkElement CreateBranch()
        {
            Line line = new Line();
            line.StrokeThickness = 1;
            line.Stroke = branchBrush;
            return line;
        }

        /// <inheritdoc />
        public void PositionBranch(FrameworkElement branch, NewickNode root,
            NewickNode node, Point rootPos, Point nodePos)
        {
            Line line = branch as Line;

            if (line == null)
            {
                throw new ArgumentException();
            }

            node.VisualElement.UpdateLayout();
            line.X1 = rootPos.X + (DefaultSettings.Default.DrawInnerNodes 
                                    ? DefaultSettings.Default.InnerNodeHeight / 2 
                                    : 0);
            line.Y1 = rootPos.Y + root.VisualElement.ActualHeight / 2;
            line.X2 = nodePos.X + (node.IsLeaf 
                                    ? DefaultSettings.Default.LeafNodeHeight / 2 
                                    : DefaultSettings.Default.DrawInnerNodes 
                                            ? DefaultSettings.Default.InnerNodeHeight / 2 
                                            : 0);
            line.Y2 = nodePos.Y + node.VisualElement.ActualHeight / 2;
        }
    }
}
